package com.starblink.imgsearch.findsimilar.ui

import android.graphics.RectF
import androidx.collection.arrayMapOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.config.PAGE_SIZE
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.basic.apollo.ext.optional
import com.starblink.android.basic.sensorsdata.helper.ImgIdentifyTrackHelper
import com.starblink.imgsearch.result.data.ImgClipModel
import com.starblink.rocketreserver.*
import com.starblink.rocketreserver.fragment.ProductF
import com.starblink.rocketreserver.type.SimilarProductSearchParam
import kotlinx.coroutines.launch

/**
 *  Create by yuanl at 2023/4/11
 **/
class FindSimilarVM : BaseViewModel() {


    var type: Int = 1
    var imgUrl: String = ""
    var productId: String = ""


    val dataLD = MutableLiveData<SearchSimilarProductByPicQuery.SearchSimilarProductByPic>()

    val imgClipData = arrayListOf<ImgClipModel>()

    val boxesList = arrayListOf<String>()

    var curBox: String? = null

    var dataLoaded = false

    val rectBoxes = arrayListOf<RectF>()

    var isLastBoxFromDrag = false

    val mainPrdMap = arrayMapOf<Int, ProductF?>()


    private val trackHelper by lazyOnNone {
        ImgIdentifyTrackHelper()
    }

    fun loadData() {
        this.viewModelScope.launch {
            trackHelper.beginIdentify()
            SkGqlClient.skQuery(SearchSimilarProductByPicQuery(
                param = SimilarProductSearchParam(
                    mainPic = imgUrl,
                    id = productId,
                    type = type,
                    pageNo = 1,
                    pageSize = PAGE_SIZE.optional(),
                )
            ))?.data?.searchSimilarProductByPic?.let {
                dataLD.postValue(it)
                dataLoaded = true
                mainPrdMap[0] = it.currentProduct?.productF
            }
            trackHelper.endIdentify()
        }
    }
}